package aps.module_Fusion;

//Vanilla Minecraft
import net.minecraft.src.GuiContainer;
import net.minecraft.src.InventoryPlayer;
import net.minecraft.src.Block;

//Forge
import net.minecraftforge.client.MinecraftForgeClient;

//APS
import aps.BuildcraftAPS;

//GL11
import org.lwjgl.opengl.GL11;

public class GuiTokamakGenerator extends GuiContainer
{
	private TileEntityTokamakGenerator tokamakInventory;
	String guiFile = BuildcraftAPS.imageFilesRoot + "TokamakGUI.png";
	
	public GuiTokamakGenerator(TileEntityTokamakGenerator tokamak, InventoryPlayer playerInv)
	{
		super(new ContainerTokamakGenerator(tokamak, playerInv));
        tokamakInventory = tokamak;
	}
	
	protected void drawGuiContainerForegroundLayer(int x, int y)
    {
        fontRenderer.drawString("Inventory", 8, (ySize - 96) + 2, 0x404040);
        String s1 = "" + (int) tokamakInventory.PowerIn;
		fontRenderer.drawString(s1, 160 - fontRenderer.getStringWidth(s1), 44, 0x404040);
		String s2 = "" + (int) tokamakInventory.PowerOut;
		fontRenderer.drawString(s2, 160 - fontRenderer.getStringWidth(s2), 57, 0x404040);
    }
	
	protected void drawGuiContainerBackgroundLayer(float f, int i, int j)
    {
        int guiTex = mc.renderEngine.getTexture(guiFile);
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        mc.renderEngine.bindTexture(guiTex);
        int topLeftX = (width - xSize) / 2;
        int topLeftY = (height - ySize) / 2;
        drawTexturedModalRect(topLeftX, topLeftY, 0, 0, xSize, ySize);
        
        //Maybe this way it'll be slightly more clear that the generator isn't ready to run on its own until it starts outputting readable power
        //instead of seemingly being stuck in "limbo" where there is no power in, or power out.
        if (tokamakInventory.PowerOut > 1) 
        	drawTexturedModalRect(topLeftX + 103, topLeftY + 20, 176, 0, 8, 8); //Fusing Indicator
        
        displayPowerGauge(topLeftX, topLeftY, 103, 44, (int) tokamakInventory.getScaledPower(false, 58)); //Power Input Gauge
        
        displayPowerGauge(topLeftX, topLeftY, 103, 57, (int) tokamakInventory.getScaledPower(true,  58)); //Power Output Gauge
        
        displayTempGauge(topLeftX, topLeftY, 85, 18, (int) tokamakInventory.getScaledTemp(58), (int) tokamakInventory.getScaledFusionTemp(58)); //Temperature Gauge
        
        displayWaterGauge(topLeftX, topLeftY, 61, 18, (int) tokamakInventory.getScaledLiquidQuantity(58));//, tokamakInventory.WaterID); //Water Level Gauge
    }
	
	private void displayWaterGauge(int TopLeftX, int TopLeftY, int GaugeTopLeftX, int GaugeTopLeftY, int BarHeight) //, int WaterId)
	{
		//System.out.println(tokamakInventory.WaterID + " " + tokamakInventory.HeavyWaterID + " " + tokamakInventory.FuelType);
		if (tokamakInventory.FuelType != 0) //provided we have a actual fuel in the generator
		{
			//Object o = Block.blocksList[tokamakInventory.FuelType];
			
			//if (o == null) {
			//	return;
			//}
			
			int liquidImgIndex;
			
			if (tokamakInventory.FuelType == tokamakInventory.WaterID)
			{
				liquidImgIndex = Block.blocksList[tokamakInventory.FuelType].blockIndexInTexture;
				mc.renderEngine.bindTexture(mc.renderEngine.getTexture("/terrain.png"));
			}
			else if (tokamakInventory.FuelType == tokamakInventory.HeavyWaterID)
			{
				liquidImgIndex = module_Fusion.heavyWater.getIconFromDamage(0);
				mc.renderEngine.bindTexture(mc.renderEngine.getTexture(module_Fusion.heavyWater.getTextureFile()));
			}
			else 
			{
				return; //unknown value
			}
			
			
			int imgLine = liquidImgIndex / 16;
			int imgColumn = liquidImgIndex - imgLine * 16;
	
			int start = 0;
	
			while (true)
			{
				int x = 0;
	
				if (BarHeight > 16) {
					x = 16;
					BarHeight -= 16;
				} else {
					x = BarHeight;
					BarHeight = 0;
				}
	
				drawTexturedModalRect(TopLeftX + GaugeTopLeftX, TopLeftY + GaugeTopLeftY + 58 - x - start, imgColumn * 16, (imgLine * 16) + (16 - x), 16, x); //The 58 is the gauge height
				start = start + 16;
	
				if (x == 0 || BarHeight == 0) {
					break;
				}
			} //This while draws the repeating gauge graphic.
		}
		mc.renderEngine.bindTexture(mc.renderEngine.getTexture(guiFile));
		drawTexturedModalRect(TopLeftX + GaugeTopLeftX, TopLeftY + GaugeTopLeftY, 176, 17, 16, 60); //This draws the gauge measurement lines on top
    }
	
	private void displayTempGauge(int TopLeftX, int TopLeftY, int GaugeTopLeftX, int GaugeTopLeftY, int BarHeight, int FusionHeight)
	{
		//MinecraftForgeClient.preloadTexture(guiFile);
		
		int start = 0;

		while (true)
		{
			int x = 0;

			if (BarHeight > 8) {
				x = 8;
				BarHeight -= 8;
			} else {
				x = BarHeight;
				BarHeight = 0;
			}

			drawTexturedModalRect(TopLeftX + GaugeTopLeftX, TopLeftY + GaugeTopLeftY + 58 - x - start, 176, 8 + (8 - x), 8, x); //The 58 is the gauge height
			start = start + 8;

			if (x == 0 || BarHeight == 0) {
				break;
			}
		} //This while draws the repeating gauge graphic.
		drawTexturedModalRect(TopLeftX + GaugeTopLeftX, TopLeftY + GaugeTopLeftY + (58 - FusionHeight), 176, 16, 8, 1); //This draws the fusion temp line on top
    }
	
	private void displayPowerGauge(int TopLeftX, int TopLeftY, int GaugeTopLeftX, int GaugeTopLeftY, int BarWidth)
	{
		//MinecraftForgeClient.preloadTexture(guiFile);
		
		int start = 0;

		while (true)
		{
			int x = 0;

			if (BarWidth > 8) {
				x = 8;
				BarWidth -= 8;
			} else {
				x = BarWidth;
				BarWidth = 0;
			}

			drawTexturedModalRect(TopLeftX + GaugeTopLeftX + start, TopLeftY + GaugeTopLeftY, 176, 8, x, 8); //The 58 is the gauge height
			start = start + 8;

			if (x == 0 || BarWidth == 0) {
				break;
			}
		} //This while draws the repeating gauge graphic.
    }
}